var numUnitShort=['','K','M','B','T']
for(var i=0;i<26;i++){
    let a1 = String.fromCharCode('A'.charCodeAt() + i)
    for(var j=0;j<26;j++){
        let a2 = String.fromCharCode('A'.charCodeAt() + j)
        numUnitShort.push(a1+a2)
    }   
}

var numUnitLong=['',
            'Thousand',
            'Million',
            'Billion',
            'Trillion',
            'Quadrillion',
            'Quintillion',
            'Sextillion',
            'Septillion',
            'Octillion',
            'Nonillion',
            'Decillion',
            'Undecillion',
            'Duodecillion',
            'Tredecillion',
            'Quattuordecillion',
            'Quinquadecillion',
            'Sedecillion',
            'Septendecillion',
            'Octodecillion',
            'Novendecillion',
            'Vigintillion',
            'Unvigintillion',
            'Duovigintillion',
            'Tresvigintillion',
            'Quattuorvigintillion',
            'Quinquavigintillion',
            'Sesvigintillion',
            'Septemvigintillion',
            'Octovigintillion',
            'Novemvigintillion',
            'Trigintillion',
            'Untrigintillion',
            'Duotrigintillion',
            'Trestrigintillion',
            'Quattuortrigintillion',
            'Quinquatrigintillion',
            'Sestrigintillion',
            'Septentrigintillion',
            'Octotrigintillion',
            'Noventrigintillion',
            'Quadragintillion',
            'Unquadragintillion',
            'Duoquadragintillion',
            'Tresquadragintillion',
            'Quattorquadragintillion',
            'Quinquaquadragintillion',
            'Sesquadragintillion',
            ]

var numUnitSci = []
numUnitSci.push("")
numUnitSci.push()
for(var i=1;i<200;i++){
    numUnitSci.push('E'+i*3)
}
var numUnit = numUnitShort
cc.Class({
    extends: cc.Label,

    properties: {
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start () {
        this.previousNum = 0
        this.time = 0
    },
    convertNumber (bigNumber) {
        if(bigNumber<1000){
            return bigNumber.toFixed(2) 
        }else{
            bigNumber = bigNumber.toFixed(0)
            let numberLen = Math.floor(Math.log10(bigNumber))
            let a = Math.floor(numberLen/3)
            let preNumber = (bigNumber / Math.pow(10,a *3)).toFixed(2) 
            let fixedNumber = preNumber + ' ' + numUnit[a]
            return fixedNumber 
        }
    },
    setNumber(numString, prefix='', suffix='',playAnime){
        this.string = prefix + this.convertNumber(Number(numString)) + suffix
        if(playAnime){
            this.labelNumberChange(numString,this.previousNum,prefix,suffix)
        }
        this.previousNum = numString        
    },
    

    labelNumberChange(changeText,originNumber,prefix,suffix){
        if(!prefix){
            prefix = ""
        }
        if(!suffix){
            suffix = ""
        }
        this.changeText = changeText
        this.prefix = prefix
        this.suffix = suffix
        this.origintargetChangeText = String(originNumber)
        
        this.labelNumberChangeDuration = 0.7
        this.labelNumberChangeStartTime = this.time
        
        if(this.node.getComponent(cc.LabelOutline).color){
            this.labelOutlineColor = this.node.getComponent(cc.LabelOutline).color
        }else{
            this.node.addComponent(cc.LabelOutline)
            this.labelOutlineColor = this.node.getComponent(cc.LabelOutline).color
        }
        
        this.node.getComponent("labelFixed").string = prefix
        this.node.runAction(cc.sequence(
            cc.scaleTo(this.labelNumberChangeDuration/2,1.2,1.2).easing(cc.easeExponentialOut()),
            cc.scaleTo(this.labelNumberChangeDuration/2,1,1).easing(cc.easeExponentialIn())
        ))
        this.start_labelNumberChange = true

    },
    _labelNumberChangeUpadate(){
        if(this.start_labelNumberChange){
            let differTime = this.time - this.labelNumberChangeStartTime
            let r = (255 - this.labelOutlineColor.r)*differTime/this.labelNumberChangeDuration + this.labelOutlineColor.r
            let g = (255 - this.labelOutlineColor.g)*differTime/this.labelNumberChangeDuration + this.labelOutlineColor.g
            let b = (255 - this.labelOutlineColor.b)*differTime/this.labelNumberChangeDuration + this.labelOutlineColor.b

            this.node.getComponent(cc.LabelOutline).color = new cc.Color(r,g,b)//labelOutlineColor.r
            let targetChangeText = String(this.changeText)
            let tmpChangeText = ""
            for(let i=0;i<targetChangeText.length;i++){
                if(this.origintargetChangeText.substring(i,i+1)== targetChangeText.substring(i,i+1)){
                    tmpChangeText+=targetChangeText.substr(i,1)
                }else{
                    tmpChangeText += Math.floor(Math.random() * 10)//targetChangeText.substr(i,1)
                }
                
            }
            this.node.getComponent("labelFixed").string = this.prefix +tmpChangeText  + this.suffix
            if(differTime >this.labelNumberChangeDuration){
                this.start_labelNumberChange = false
                this.node.getComponent("labelFixed").string = this.prefix + this.convertNumber(Number(targetChangeText)) + this.suffix
                this.node.getComponent(cc.LabelOutline).color = this.labelOutlineColor
            }
        }
    },
    update (dt) {
        this.time+=dt
        this._labelNumberChangeUpadate()
    },
});
